<template>
	<div class="widgets bg-white">
		<el-input v-model.trim="searchText" placeholder="输入搜索内容" class="search__input" @keyup.enter.native="search"></el-input>
        <el-button type="primary" @click="search">查询</el-button>
        <div class="btn-group right">
	        <el-button type="info" @click="refresh">刷新</el-button>
	        <el-button type="primary" @click="add">新增</el-button>
	        <el-button type="danger" v-if="multipleSelection.length <= 0" disabled>删除</el-button>
	        <el-button type="danger" v-else @click="del">删除</el-button>
	        <el-button type="danger" v-if="multipleSelection.length <= 0" disabled>禁用</el-button>
	        <el-button type="danger" v-else @click="disabedBtn(0, '禁用')">禁用</el-button>
	        <el-button type="success" v-if="multipleSelection.length <= 0" disabled>启用</el-button>
	        <el-button type="success" v-else @click="disabedBtn(1, '启用')">启用</el-button>
	        <el-button-group class="btn-other">
	            <el-button v-if="multipleSelection.length <= 0" disabled>
	                <img src="../../../assets/images/export.png" alt="">
	            </el-button>
	            <el-button title="导出" v-else @click="exportExcel">
	                <img src="../../../assets/images/export.png" alt="">
	            </el-button>
	            <el-button title="导入">
                    <el-upload class="upload-demo" :action="$url + 'accountItem/import?superiorId=' + (row.length > 0 && row[0]['id'])" :show-file-list="false" multiple style="display: inline-block;" :on-success="importExcelSuccess">
                        <img src="../../../assets/images/import.png" alt="">
                    </el-upload>
                </el-button>                 
	            <el-button title="打印">
	                <img src="../../../assets/images/print.png" alt="">
	            </el-button>
	            <el-button title="帮助">
	                <img src="../../../assets/images/help.png" alt="">
	            </el-button>
	        </el-button-group>
	    </div>
	</div>
</template>
<script>
export default {
	data() {
		return {
			searchText: '',
			multipleSelection: this.$store.state.checkItemsStore.multipleSelection,
			selectRowId: this.$store.state.checkItemsStore.selectRowId,
			row: this.$store.state.checkItemsStore.row,
            searchResult: []
		}
	},
    watch: {
        searchText(newVal) {
            if(!newVal) {
                let expandNodeId = this.$store.state.checkItemsStore.expandNodeId;
                this.$store.commit('checkItemsStore/updateExpandNodeId', expandNodeId[0]);
                this.$store.dispatch('checkItemsStore/getTreeData');
            }
        }
    },
	methods: {
		search() {
            if(this.searchText) {
                this.filterData(this.$store.state.checkItemsStore.treeData);
                let newArr = [],
                    data = this.searchResult,
                    reg = new RegExp(this.searchText, "g");
                data.forEach(item => {
                    if(reg.test(item['name']) || reg.test(item['remakes']) || reg.test(item['mnemonicCode']) || reg.test(item['superiorName']) || (reg.test(item['mnemonicCodeToPinYin'])) || (item['nameToPinYin'] && reg.test(item['nameToPinYin'])) || (item['remakesToPinYin'] && reg.test(item['remakesToPinYin'])) || (item['superiorNameToPinyin'] && reg.test(item['superiorNameToPinyin']))) {
                        newArr.push(item);
                    }
                });
                this.$store.commit('checkItemsStore/updateTableData2', newArr);
            }
        },
        filterData(arr) {
            if(arr.length > 0) {
                arr.forEach(item => {
                    this.searchResult.push(item);
                    let accountItems = item['accountItems'];
                    if(accountItems && accountItems.length > 0) {
                        this.filterData(accountItems);
                    }
                });
            }
        },
		refresh() {			//刷新
			const loading = this.$loading({
                lock: true,
                text: '数据刷新中...',
                spinner: 'el-icon-loading',
                background: '#fff'
            });
            setTimeout(() => {
                loading.close();
            }, 1000);
		},
		add() {			//新增
			this.$store.commit('checkItemsStore/updateRowId');
			this.$util.openLayerIframe({title: '核算项目-新增', zIndex: 13, width: '550px', height: '410px', url: this.$domain + '/#/basicData/checkItems/form'});
		},
		del() {			//删除
            let arr = this.multipleSelection;
            arr.forEach(item => {
                if(item['accountItems'].length > 0) {
                    this.$message({message: '删除失败，该节点包含子节点', duration: 1500, type: 'warning'});
                    return false;
                }
                this.$confirm('确定删除选中数据?', '删除提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$http.delete('accountItem/delete', {data: this.selectRowId}).then(res => {
                        if(res.data.code == 20001) {
                            this.$message({message: '删除成功', duration: 1500, type: 'success'});
                            //更新要展开高亮的节点id
                            this.$store.commit('checkItemsStore/updateExpandNodeId', this.multipleSelection[0]['superiorId']);
                            setTimeout(() => {
                                this.$store.dispatch('checkItemsStore/getTreeData');            //刷新数据
                            }, 1000);
                        } else {
                            this.$message.error(res.data.msg || '删除失败');
                        }
                    });
                }).catch(() => {
                    this.$message.info('已取消删除');
                });
            });
		},
		disabedBtn(status, msg) {			//禁用，启用
			var type = {
                headers: {
                    'Content-type': 'application/json;charset=UTF-8'
                }
            }
            for(var i = 0; i < this.multipleSelection.length; i++) {
                if(this.multipleSelection[i]['state'] == status) {
                    this.$alert(msg + '失败，其中包含已'+ msg +'选项，重新选择！', msg + '提示', {
                        confirmButtonText: '确定'
                    });
                    return false;
                }
            }
            this.$http.post('accountItem/updateSate?state=' + status, JSON.stringify(this.selectRowId), type).then(res => {
                if(res.data.code == 20001) {
                    this.$message({message: msg + '成功', type: 'success', duration: 2000});
                    //更新要展开高亮的节点id
                    this.$store.commit('checkItemsStore/updateExpandNodeId', this.multipleSelection[0]['superiorId']);
                    setTimeout(() => {
                        this.$store.dispatch('checkItemsStore/getTreeData');
                    }, 1000);
                }
            });
		},
		exportExcel() {			//导出
			if(this.multipleSelection.length > 0) {
                var idSting = this.selectRowId.join(',');           //将[1,2,3]格式转为 "1,2,3"
                this.$http.get('accountItem/export',{params: { idStrs: idSting }}).then(res => {
                    this.$message({message: '导出成功', type: 'success'});
                    window.location.href = this.$url + 'accountItem/export?idStrs=' + idSting;          //导出时触发
                }).catch(err => {
                    this.$message.error(err);
                });
            }
		},
		importExcelSuccess(res) {               //导入成功后触发
            if(res.code === 20001) {
                this.$message({message: '导入成功', type: 'success'});
                setTimeout(() => {
                    this.$store.dispatch('checkItemsStore/getTreeData');
                }, 1000);
            }
        }
	}
}
</script>